<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="媒体类型" prop="mediaType">
        <el-select v-model="queryParams.mediaType" placeholder="请选择" @change="changeMedia">
          <el-option v-for="(dict, index) in mediaTypeOption" :key="index" :label="dict.label"
                     :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="转换前格式" prop="beforeFormat">
        <el-select v-model="queryParams.beforeFormat" placeholder="请选择" @click.native="changeTip">
          <el-option v-for="(dict, index) in formatOption" :key="index" :label="dict.label"
                     :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="转换后格式" prop="afterFormat">
        <el-select v-model="queryParams.afterFormat" placeholder="请选择" @click.native="changeTip">
          <el-option v-for="(dict, index) in formatOption" :key="index" :label="dict.label"
                     :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPermi="['archive:gszh:add']">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" @click="handleUpdate" v-hasPermi="['archive:gszh:update']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" @click="handleDelete" v-hasPermi="['archive:gszh:delete']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :search="false"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" :height="tableHeight - 19"
              border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="id"/>
      <el-table-column label="转换前格式" align="center" prop="beforeFormat" :show-overflow-tooltip="true" />
      <el-table-column label="转换后格式" align="center" prop="afterFormat" :show-overflow-tooltip="true" />
      <el-table-column label="媒体类型" align="center" prop="mediaType" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ scope.row.state == '1' ? '启用' : '禁用' }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <vDeit :open="open" :info="info" @closeDialog="closeDialog"></vDeit>
  </div>
</template>

<script>

import {deleteList, getFormatType, getRule, list} from "@/api/formatConversion/formatConversion.js";
import estimateTableHeight from '@/mixins/estimateTableHeight';
import vDeit from "@/views/formatConversion/formatConversion/deit.vue"
import {delFONDS} from "@/api/manage/general";
export default {
  components:{
    vDeit
  },
  mixins: [estimateTableHeight], // 混入方法
  name: "Dict",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mediaType: undefined,
        beforeFormat: undefined,
        afterFormat: undefined
      },
      valueTime: "", //时间范围
      formatOption: [],
      mediaTypeOption: [],

      open: false,
      info: {}
    };
  },
  created() {
    this.getList();
    this.getOption()
  },
  methods: {
    getOption() {
      getFormatType({
        pId: '0'
      }).then(res=>{
        this.mediaTypeOption = res.rows.map(item=>{
          return {
            value: item.id,
            label: item.format
          }
        })
      })
    },
    changeMedia(value) {
      this.queryParams.beforeFormat = undefined
      this.queryParams.afterFormat = undefined
      var str = this.mediaTypeOption.find(item => item.label == value).value
      getFormatType({
        pId: str
      }).then(res=>{
        this.formatOption = res.rows.map(item=>{
          return {
            value: item.id,
            label: item.format
          }
        })
      })
    },
    // 添加提示
    changeTip() {
      if (this.queryParams.mediaType == undefined) {
        this.$modal.msgWarning('请先选择媒体类型')
      }
    },
    table_index(index){
      return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 新增
    handleAdd() {
      this.info = {}
      this.open = true
    },
    // 修改
    handleUpdate() {
      if (this.ids.length > 0) {
        if (!this.single) {
          getRule(this.ids.toString()).then(res=>{
            this.info = res.data
            this.open = true
          })
        } else {
          this.$message({ message: '只能选择一条修改的数据!', type: 'warning' })
          return false
        }
      } else {
        this.$message({ message: '请选择数据进行修改!', type: 'warning' })
        return false
      }
    },
    closeDialog() {
      this.open = false
      this.getList()
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (!this.multiple) {
        this.$modal.confirm('是否确认删除数据项？').then(function () {
          return deleteList(ids.toString())
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      } else {
        this.$message({ message: '只能选择要删除的数据!', type: 'warning' })
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download('/archive/toOfd/export', {
      //   ...this.queryParams
      // }, `type_${new Date().getTime()}.xlsx`)
    },
    //搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList();
    },

    //重置
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.resetForm('queryForm')
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        mediaType: undefined,
        beforeFormat: undefined,
        afterFormat: undefined
      }
      this.formatOption = []
      this.getList();
    }
  }
};
</script>
